<?php
namespace App\Http\Controllers\Business\Shop\V1\CommentManagement;

use App\Models\Comment;
use App\Models\Order;
use App\Models\Shop;
use App\Models\ShopUser;
use App\Models\User;
use Illuminate\Support\Facades\Redis;

class CommentManagement{
    //评论列表
    public static function commentList($shopId,$type = 'all', $page=1, $length=20){
        if(!$shopId|| !$type || !(($type == 'all') || ($type == 'bad'))) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $skip = ($page-1)*$length;
        if($type == 'all') {
            $count =  Comment::where('shopid', $shopId)->where('isdel', '0')->get()->count();
            $lists = Comment::where('shopid', $shopId)->where('isdel', '0')->orderBy('created_at', 'desc')
                ->select('nickname', 'useravatar', 'created_at', 'commenttime','id as commentid', 'score', 'content', 'picture','replycontent', 'reportstatus')
                ->skip($skip)->take($length)->get();
        } else {
            $count =  Comment::where('shopid', $shopId)->where('isdel', '0')->where('score', '<=', 5)->get()->count();
            $lists = Comment::where('shopid', $shopId)->where('isdel', '0')->where('score', '<=', 5)
                ->orderBy('comments.created_at', 'desc')
                ->select('nickname', 'useravatar', 'created_at','commenttime', 'id as commentid', 'score', 'content', 'picture','replycontent', 'reportstatus')
                ->skip($skip)->take($length)->get();
        }
        $data = [
            'count' => $count,
            'lists' => $lists
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //新增回评
    public static function replyComment($shopId,$commentId,$replyContent){
        if(!$shopId || !$commentId || !$replyContent) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $comment = Comment::where('id',$commentId)->where('shopid',$shopId)->first();
        if(!$comment) {
            return ['status'=>'failed','message'=>'回复失败'];
        }
        if($comment->replycontent) {
            return ['status'=>'failed','message'=>'回复失败，不能重复回复！'];
        }
        $comment->replycontent = $replyContent;
        $comment->replytime = date('Y-m-d H:i:s');
        $comment->save();
        return ['status'=>'success','message'=>'回复成功'];
    }
    //新增举报
    public static function reportComment($shopId,$commentId,$reportContent) {
        if(!$shopId || !$commentId || !$reportContent) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $comment = Comment::where('id',$commentId)->where('shopid',$shopId)->first();
        if(!$comment) {
            return ['status'=>'failed','message'=>'举报失败'];
        }
        if($comment->reportstatus != 0) {
            return ['status'=>'failed','message'=>'举报失败，不能重复举报！'];
        }
        $comment->reportstatus = 1;
        $comment->reportcontent = $reportContent;
        $comment->reporttime = date('Y-m-d H:i:s');
        $comment->save();
        return ['status'=>'success','message'=>'举报成功'];
    }
    //举报列表
    public static function reportList($shopId, $page=1, $length=20) {
        if(!$shopId) {
            return ['status'=>'failed','message'=>'参数错误！'];
        }
        $skip = ($page-1)*$length;
        $reportList = Comment::where('shopid', $shopId)->where('reportstatus', '<>', 0)->orderBy('reporttime', 'desc')
            ->select('nickname', 'id as commentid', 'content', 'reportstatus', 'reportcontent', 'reporttime')
            ->skip($skip)->take($length)->get();
        $count = Comment::where('shopid', $shopId)->where('reportstatus', '<>', 0)->get()->count();
        $data = [
            'count' => $count,
            'lists' => $reportList
        ];
        return ['status'=>'success', 'data'=>$data];
    }
}